// pages/storeItemDetail/storeItemDetail.js
const app = getApp();
import api from '../../../utils/util.js';
var WxParse = require('../../../wxParse/wxParse.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    fullHeight: app.globalData.fullcontain,
    navmess: {
      navtitle: "产品详情",
      backhide: 0,
      topheight: 1,
    },
    swiperSetting:{
      indicatorDots: true,
      vertical: false,
      autoplay: false,
      interval: 2000,
      duration: 500,
    }
  },
  pageto: function(e){
    var url = e.currentTarget.dataset.url,
        data = e.currentTarget.dataset.datobj;
    app.goto(url,data);
  },
  // 立即购买（加入购物车）
  addcart: function(){
    var that = this;
    var params = {
      "token": that.data.token,
      "enterpriseNo": that.data.ent,
      "goodsType": 1,
      "goodsId": that.data.goodId,
      "quantity": 1
    }
    api.post("/mini/my/cart/joinCart",params).then(res =>{
      if (res.code == 200) {
          console.log(res)
          wx.navigateTo({
            url: "../storeBuyNow/storeBuyNow"
          })
      }
    }).catch(res => {
      console.log("请求失败：",res);
      return false;
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log("商品sign：",JSON.parse(options.sign)); //获取sign
    var params = {
      sign: JSON.parse(options.sign)
    }
    api.post("/mini/goods/productDetail",params).then(res =>{
      if (res.code == 200) {
        that.setData({
          demolist: res.result,
          goodId: res.result.id
        })
        console.log(res.result)
        // 富文本解析初始化数据
        WxParse.emojisInit('[]', "../../../wxParse/emojis/", {
          "00": "00.gif",
          "01": "01.gif",
          "02": "02.gif",
          "03": "03.gif",
          "04": "04.gif",
          "05": "05.gif",
          "06": "06.gif",
          "07": "07.gif",
          "08": "08.gif",
          "09": "09.gif",
          "09": "09.gif",
          "10": "10.gif",
          "11": "11.gif",
          "12": "12.gif",
          "13": "13.gif",
          "14": "14.gif",
          "15": "15.gif",
          "16": "16.gif",
          "17": "17.gif",
          "18": "18.gif",
          "19": "19.gif",
        });
        var article = that.data.demolist.detailWechat;
        /*** WxParse.wxParse(bindName , type, data, target,imagePadding)
        * 1.bindName绑定的数据名(必填)
        * 2.type可以为html或者md(必填)
        * 3.data为传入的具体数据(必填)
        * 4.target为Page对象,一般为this(必填)
        * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)*/ 
        var temp = WxParse.wxParse('article', 'html', article, that, 5);
      }
    }).catch(res => {
      console.log("请求失败：",res);
      return false;
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if(app.checkLogin()){
      var token = wx.getStorageSync('token');
      this.setData({
        isLogin: app.checkLogin(),
        token: token
      })
    }
    api.post("/mini/my/cart/simpleCart",{
      "token": that.data.token
    }).then(res =>{
      if (res.code == 200) {
        that.setData({
          cartNum: res.result.count
        })
        console.log(res.result)
      }
    }).catch(res => {
      console.log("请求失败：",res);
      return false;
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})